package com.zqwm.fly.task;

import com.zqwm.fly.mapper.ArticleMapper;
import com.zqwm.fly.model.Article;
import com.zqwm.fly.mongo.RecipeMongodb;
import com.zqwm.fly.utils.DateUtil;
import com.zqwm.fly.utils.HttpUtils;
import org.htmlparser.NodeFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.htmlparser.Parser;
import org.htmlparser.util.NodeList;

import java.net.URI;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class ArticleTask
{
    private static final Logger logger = LoggerFactory.getLogger(ArticleTask.class);
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private RecipeMongodb recipeMongodb;



    //定时执行菜谱
    @Scheduled(cron = "0 56 11 * * ?")
    public void executeUpdateCaipu()throws Exception
    {
        System.out.println("菜谱-------");
        String[]lucai ={
                "红烧鱼",
                "红烧火腩",
                "红烧栗子鸡",
                "红烧全狗",
                "红烧蹄筋",
                "红烧五",
                "红烧鸭",
                "红烧鱼唇",
                "红烧猪",
                "红烧猪手",
                "红酥小鱼",
                "红油耳片",
                "红糟片鸭",
                "红枣煨",
                "红糟鱼丝",
                "蝴蝶海参",
                "蝴蝶烩",
                "葫大吉翅子",
                "湖南",
                "虎皮毛豆腐",
                "虎皮梳子",
                "虎皮",
                "鸡",
                "果鱼",
                "滑",
                "式绣球酥",
                "虾托",
                "焖甲鱼",
                "焖",
                "泥煨鸡",
                "徽",
                "回锅牛小排",
                "回锅",
                "徽州园子",
                "火爆腰块",
                "火腿炖甲鱼",
                "火腿酥腰",
                "鸡火蹄燕",
                "鸡粒盒",
                "吉利虾",
                "鸡茸笔架鱼",
                "鸡茸杏",
                "鸡丝炒耳块",
                "鸡丝燕窝",
                "鸡汤氽海",
                "鸡汤煮千丝",
                "鸡汁打绣球",
                "家常豆腐",
                "家乡",
                "佳大盘鸡",
                "煎扒鱼头尾",
                "酱爆青",
                "姜爆三丝",
                "姜葱炒",
                "姜葱煲",
                "酱丝",
                "江",
                "姜汁赤贝",
                "酱汁排",
                "姜汁热味鸡",
                "酱汁鸭方",
                "叫化鸡",
                "叫化仔鸡",
                "椒油扁豆",
                "包",
                "挂玉牌",
                "鸡塔",
                "丝玉兔",
                "腿脊梅炖腰酥",
                "镶玉汤",
                "鱼戏",
                "荆沙鱼糕",
                "丝墨鱼汤",
                "酒酿腿",
                "桔瓣广昌",
                "桔瓣鱼汆",
                "火锅",
                "虎凤羹",
                "鱼",
                "鱿鱼",
                "聚三",
                "蕨根粉煮鱼",
                "咖哩牛",
                "怀水白",
                "烤牌子",
                "烤乳猪",
                "客家封鸡",
                "空心鱼丸汤",
                "口感盐煎",
                "口水鸡",
                "口味牛蹄",
                "口味片",
                "瓜酿",
                "炒猪豆腐",
                "豉兔",
                "豆瓣鱼",
                "椒鱼",
                "腊炒",
                "味江鸡",
                "汁酱",
                "汁泥",
                "子鸡丁",
                "子鱼块",
                "赖汤圆",
                "兰大虾",
                "篮球鸽",
                "冷冻鸭掌",
                "荔枝",
                "栗子焖羊",
                "理双味鱼",
                "子鸡丁",
                "凉拌鱿鱼",
                "凉瓜排",
                "灵恋玉蝉",
                "六百居粉",
                "流青",
                "琉璃",
                "溜牛脊",
                "熘鱼焙面",
                "抄手",
                "凤配",
                "凤腿",
                "虎斗",
                "井虾仁",
                "凤尾虾",
                "须燕丸",
                "眼虾仁",
                "爪丝",
                "爪鱼翅",
                "卤豆腐",
                "陆稿荐卤",
                "箩粉鱼头豆腐汤",
                "罗锅",
                "冬笋",
                "子鸡",
                "婆豆腐",
                "仁酥鸭",
                "酥鸭",
                "上树",
                "油鸡",
                "毛峰熏鱼",
                "毛血旺",
                "梅扣",
                "美人",
                "梅子排",
                "蜜豆鱼片",
                "蜜枣桂鱼",
                "秘制豉排",
                "生果",
                "魔锅巴炒丝",
                "魔鸭块",
                "奶汤鸡脯",
                "奶汤元鱼",
                "奶油烤萝什锦",
                "奶汁回王鱼",
                "南瓜百合爽",
                "南湖粉",
                "南瓶晚",
                "泥豆腐",
                "牛火锅",
                "牛筋煲",
                "牛腩煲水煮牛",
                "浓汁鹌鹑",
                "糯米油稣鸡",
                "泡椒响片",
                "培养正气鸡块",
                "皮烩排",
                "啤酒椒鸡",
                "琵琶鱼",
                "皮条鱼",
                "平锅羊",
                "萍乡驴",
                "鱼",
                "杞忧烘皮",
                "千岛湖红烧鱼头",
                "炝鱿鱼卷",
                "炝糟五",
                "茄汁明虾",
                "茄子煮甲",
                "烧鱼条",
                "鱼丝",
                "清炒三丁",
                "清炒虾仁",
                "清炖狼山鸡",
                "清炖兔子",
                "清炖武山鸡",
                "清炖粉狮子头",
                "清汤鱼",
                "清汤鱼",
                "清汤柳叶燕",
                "清汤耳素鸽",
                "清汤玉带鸡",
                "清匀鱼",
                "清桂鱼",
                "清荷包红鱼",
                "清鱼",
                "清鱼",
                "清青",
                "清鱼",
                "清武昌鱼",
                "清西瓜鸡",
                "清鱼",
                "全家富",
                "全虾三做",
                "巢椒牛柳",
                "瓤豆腐",
                "热白豆腐",
                "茸汤广",
                "米鱼唇",
                "脯卷",
                "碎豉椒炒豇豆",
                "软炸石鸡",
                "瑞士排",
                "三杯鸡",
                "三冬片",
                "三虾豆腐",
                "子豆",
                "砂锅核",
                "砂锅三味",
                "沙律海卷",
                "沙窝椒汁大鱼头",
                "山城第伀腿",
                "山东",
                "珊瑚枝片",
                "珊瑚",
                "山峡味烤鱼",
                "单县羊汤",
                "烧罗汉面筋",
                "烧三不粘",
                "烧子",
                "神仙鸭子",
                "生炒海",
                "生敲丝",
                "生熏大鱼",
                "石耳炖鸡",
                "石斛生米",
                "石家酱方",
                "什锦冬瓜帽",
                "什锦海参",
                "狗",
                "石榴鸡",
                "手撕鸡",
                "梳子百页",
                "双鱼片",
                "双皮匀鱼",
                "双仁浮皮",
                "双色",
                "水果鸡",
                "水晶",
                "水煮回头鱼",
                "水煮牛",
                "水煮片",
                "水煮鱼",
                "四宝上汤",
                "四川泡",
                "松仁粉丝",
                "松鼠桂鱼",
                "松鼠鱼",
                "松子河虾仁",
                "松子茄鱼",
                "松子鱼米",
                "素炒鸡丁",
                "素烩",
                "酥盐鸡块",
                "酥炸禾",
                "酥炸鸭子",
                "酸牛",
                "酸小豆汤",
                "酸鱼",
                "酸煮鱼",
                "酸凤爪",
                "酸狗",
                "酸海参锅巴",
                "酸鱿鱼丝",
                "味爽",
                "笋丝百叶",
                "明虾",
                "干羊丝",
                "太极鱼翅",
                "台式三杯鸡",
                "太爷鸡",
                "坛子",
                "糖炒红泥",
                "糖咕噜",
                "糖鱼",
                "糖软熘河鱼",
                "糖软熘鱼焙面",
                "糖猪腰",
                "糖酱鱼",
                "套四宝",
                "扒仔鸡",
                "板鱿鱼须",
                "锅",
                "桐",
                "氽脊脑",
                "屯溪",
                "式鱼",
                "外婆大品碗",
                "网包鱼",
                "文山鸡丁",
                "笋叶子",
                "爆脊",
                "五彩炒丝",
                "五丁鱼圆",
                "五夫鸡",
                "五柳青鱼",
                "五味煎",
                "无锡嫩子排",
                "无锡排",
                "五豆腐干丝",
                "西湖鱼",
                "虾米炒瓜",
                "虾鸳鸯",
                "咸鸭",
                "仙鹤望桥",
                "溜鸡丁",
                "奶蘑",
                "咸烧白",
                "味大虾",
                "湘",
                "兰",
                "乡村小炒鸡",
                "烧心",
                "煎翘鱼",
                "响圆",
                "酥凤腿",
                "酥鸡",
                "酥素鸭块",
                "扣",
                "粽烧排",
                "小桃园煨汤",
                "潇湘五元",
                "粉豆腐羹",
                "虾盅",
                "西兰",
                "杏仁炖",
                "盐回锅",
                "腌炒",
                "盐水鸭",
                "盐酸烧鱼",
                "腌鱼",
                "杨梅虾球",
                "洋烧排",
                "山椒河炖猪脚",
                "夜冬瓜盅",
                "椰咖喱鸡",
                "鸭饭",
                "宜宾燃面",
                "伀品鱼",
                "伀品",
                "伀品豆腐",
                "伀品豆腐汤",
                "伀品酸鸡",
                "伀品鱼",
                "包",
                "耳川鸭",
                "鸡丝",
                "牙",
                "鱼丝",
                "鱼笀卤豆",
                "樱桃",
                "营养青",
                "油爆",
                "油爆尖",
                "油爆双",
                "油爆虾",
                "油爆小虾",
                "油浸蝶鱼",
                "油淋鸽子",
                "油淋鸡",
                "油泡鱼青丸",
                "鱿鱼三",
                "禹城扒鸡",
                "鱼米羹",
                "鱼腩煲",
                "鱼大虾",
                "鱼荷包",
                "鱼丝",
                "鱼羊炖时蔬",
                "鱼羊",
                "鱼咬羊",
                "原笼粉",
                "鸳鸯鱼",
                "鸳鸯鱖鱼",
                "粤",
                "云托八",
                "枣方",
                "糟脆皮鱼",
                "糟熘鱼片",
                "糟煨冬笋",
                "炸八块",
                "炸",
                "炸鸡排",
                "炸碌烹",
                "炸石鸡",
                "炸素鸡排",
                "炸脂盖",
                "樟茶鸭",
                "樟茶鸭子",
                "浙",
                "折耳根炒丝",
                "珍珠豆腐",
                "珍珠南瓜",
                "纸包三",
                "田鸡",
                "中华酱",
                "猪煲咸",
                "猪水",
                "竹鸡盅",
                "竹笋汤",
                "紫梅鸡",
                "祖庵鱼翅",
                "组庵鱼翅",
                "大转弯",
                "鹅掌",
                "排",
                "清炖鸡"
        };
        int lucailenth = lucai.length;
        int sum = 0;
        for(int c=0;c<lucailenth;c++)
        {
            HttpUtils httpUtils = new HttpUtils();
            String url = URLEncoder.encode("http://apis.juhe.cn/cook/query.php?menu="+lucai[c].trim().replaceAll(" ", "")+"&dtype=&pn=&rn=&albums=&=&key=5e3c07c1cff0b63d0ba97fa9283c5575").replaceAll("\\+","%20");
            System.out.println(URLDecoder.decode(url));
            String json = httpUtils.get(URLDecoder.decode(url));
            System.out.println(json);
            if(new JSONObject(json).getInt("resultcode") == 200)
            {
                sum = sum + new JSONObject(json).getJSONObject("result").getInt("totalNum");
                JSONArray jsonArray = new JSONObject(json).getJSONObject("result").getJSONArray("data");
                int jsonLenth = jsonArray.length();
                for (int i =0 ;i<jsonLenth;i++)
                {
                    //recipeMongodb.insert(jsonArray.getJSONObject(i));
                }
            }

            System.out.println(json);
        }
        System.out.println(sum+"条");

    }

    //从网易艺术模块下载数据
    @Scheduled(cron="0 57 23 * * ?")
    public void executeUpdateArticle()throws Exception
    {
        //http://art.163.com/special/00999815/art_redian_api_02.js?callback=data_callback
        String json = HttpUtils.httpGet("http://art.163.com/special/00999815/art_redian_api.js?callback=data_callback",null,"GBK");
        json = json.replace("data_callback(","").replace(")","").replace("\n","").trim();

        JSONArray jsonArray = new JSONArray(json);

        for (int i=0; i<jsonArray.length();i++)
        {
            Article article = new Article();
            JSONObject jsonObject = (JSONObject) jsonArray.get(i);

            article.setTitle(jsonObject.getString("title"));
            article.setPublishtime(DateUtil.parseDate(jsonObject.getString("time"),"MM/dd/yyyy HH:mm:ss"));
            article.setCoverPicture(jsonObject.getString("imgurl"));
            article.setLabel(jsonObject.getString("label"));

            System.out.println("首页图片："+jsonObject.getString("imgurl"));
            System.out.println("标题："+jsonObject.getString("title"));
            System.out.println("文章详细页："+jsonObject.getString("docurl"));
            System.out.println("时间："+jsonObject.getString("time"));
            System.out.println("标签："+jsonObject.getString("label"));

            String html = HttpUtils.httpGet(jsonObject.getString("docurl"),null,"utf-8");
            Parser parser1 = Parser.createParser(html, "UTF-8");
            NodeFilter divList = new HasAttributeFilter( "class", "time" );
            NodeList  divnodeList = parser1.extractAllNodesThatMatch(divList);

            if(divnodeList.size()>0)
            {
                System.out.println("来源："+divnodeList.elementAt(0).getChildren().elementAt(5).toPlainTextString());
                article.setSource(divnodeList.elementAt(0).getChildren().elementAt(5).toPlainTextString());

                //判断是否是今天的文章信息
                if(article.getPublishtime().getTime()-DateUtil.getDayStartTime().getTime()>=0)
                {
                    parser1 = Parser.createParser(html,"UTF-8");
                    divList = new HasAttributeFilter( "class", "content" );
                    NodeList contentNode = parser1.extractAllNodesThatMatch(divList);
                    String content = contentNode.toHtml().replace("\t","").replace("\n","").replace("\r","").replace("\\s","").trim();
                    System.out.println("内容："+content);
                    article.setContent(content.replace("<img","<img class=\"img-responsive center-block\" "));//设置图片居中显示
                }
            }else{
                parser1 = Parser.createParser(html, "UTF-8");
                NodeFilter divLists = new HasAttributeFilter( "class", "post_time_source" );
                NodeList divnodeLists = parser1.extractAllNodesThatMatch(divLists);

                if(null != divnodeLists)
                {
                    String [] dateAndSoure = divnodeLists.elementAt(0).toPlainTextString().trim().split("　来源:");

                    //判断是否是今天的文章信息
                    if(article.getPublishtime().getTime()-DateUtil.getDayStartTime().getTime()>=0)
                    {
                        parser1 = Parser.createParser(html,"UTF-8");
                        divLists = new HasAttributeFilter( "id", "endText" );
                        divnodeLists = parser1.extractAllNodesThatMatch(divLists);
                        String content = divnodeLists.toHtml().replace("\t","").replace("\n","").replace("\r","").replace("\\s","").trim();
                        System.out.println("内容："+content);
                        article.setContent(content.replace("<img","<img class=\"img-responsive center-block\" "));//设置图片居中显示
                        //String coverPicture = HtmlParserUtil.changeImgSrc(content);
                        System.out.println(divnodeList);
                    }
                }
            }

            if(null != article.getContent()){ articleMapper.insert(article); }
        }

    }

    //从有道下载数据
    //https://dict.youdao.com/infoline?apiversion=2&mode=publish&client=deskdict&keyfrom=dict2.index&startDate=2017-09-26&callback=vistaCallback
    @Scheduled(cron="0 57 23 * * ?")
    public void youDaoUpdateArticle()throws Exception
    {
        //获取今天的时间
        String today = DateUtil.format(new Date(),"yyyy-MM-dd");
        //http://art.163.com/special/00999815/art_redian_api_02.js?callback=data_callback
        String json = HttpUtils.httpGet("https://dict.youdao.com/infoline?apiversion=2&mode=publish&client=deskdict&keyfrom=dict2.index&startDate="+today+"&callback=vistaCallback",null,"GBK");
        json = json.replace("vistaCallback(","").replace(")","").trim();

        JSONObject jsonObject = new JSONObject(json);
        JSONArray jsonArray = jsonObject.getJSONArray(today);
        for (int i=0; i<jsonArray.length();i++)
        {
            Article article = new Article();
            JSONObject obj = jsonArray.getJSONObject(i);
            article.setTitle(obj.getString("title"));
            article.setPublishtime(new Date());
            article.setLabel(obj.getString("cardTag"));
            article.setSource(obj.getString("type"));
            article.setCoverPicture(obj.getString("translatorImg"));

            //添加文章内容http://xue.youdao.com/sw/m/1468475?showmethod=native&keyfrom=wap&client=wap&_=1506504872354&callback=jsonp1


            String stringContent = HttpUtils.httpGet(obj.getString("url")+"?showmethod=native&keyfrom=wap&client=wap&_=1506504872354&callback=jsonp1",null,"utf-8");
            stringContent = stringContent.replace("jsonp1(","").replace(")","").trim();
            JSONObject jsonContent = new JSONObject(stringContent);
            article.setContent(jsonContent.getString("content").replace("\t","").replace("\n","").replace("\r","").replace("\\s","").replace("<img","<img class=\"img-responsive center-block\" ").trim());

            //if(null != article.getContent()&& !"".equals(article.getContent())){ articleMapper.insert(article); }

            System.out.println("首页图片："+article.getCoverPicture());
            System.out.println("标题："+article.getTitle());
            System.out.println("文章内容："+article.getContent());
            System.out.println("时间："+article.getPublishtime());
            System.out.println("标签："+article.getLabel());
            System.out.println("来源："+article.getSource());
        }
    }

    public static void main(String[] args) {

        Pattern p = Pattern.compile("\\s*|\t|\r|\n");
        Matcher m = p.matcher("金针肉 　 　      "+"s");
        String dest = m.replaceAll("");
        System.out.println(dest.replaceAll("\\u000d","s").replaceAll("s","y").replaceAll("\\u0009","k").replaceAll("\\u000a","j").trim());
    }
}
